import Vue from 'vue';
import App from './App.vue';
import router from './router';
import './router/intercept.js';
import routerServer from './utils/routerServer';
import store from './store';
import './assets/css/reset.css';
import ElementUI, { Message } from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
import Cookies from 'js-cookie';
import { UTable, UTableColumn } from 'umy-ui';
Vue.use(ElementUI);
import http from './utils/http';
import filters from './filter/index';
import data from './store/data';
import './components/index.js'

import './assets/global/layout/index.css'
import installTinymce from './components/Tinymce/index';
Vue.use(installTinymce)

Vue.use(filters);
import * as echarts from 'echarts';

Vue.prototype.$echarts = echarts

import { getToken } from '@/utils/auth'

import jquery from 'jquery'
Vue.prototype.$ = jquery
// 为了实现Class的私有属性
const showMessage = Symbol('showMessage')
import iconPicker from 'vue-fontawesome-elementui-icon-picker';

Vue.use(iconPicker);

import VideoPlayer from 'vue-video-player'
import 'vue-video-player/src/custom-theme.css'
import 'video.js/dist/video-js.css'
Vue.use(VideoPlayer)
/** 
 *  重写ElementUI的Message
 *  single默认值true，因为项目需求，默认只弹出一个，可以根据实际需要设置
 */
let messageInstance = null;
class DonMessage {
  success (options, single = true) {
    this[showMessage]('success', options, single)
  }
  warning (options, single = true) {
    this[showMessage]('warning', options, single)
  }
  info (options, single = true) {
    this[showMessage]('info', options, single)
  }
  error (options, single = true) {
    this[showMessage]('error', options, single)
  }
  [showMessage] (type, options, single) {
    if (messageInstance && single) {
      messageInstance.close()//先把原来的关闭
    }
    messageInstance = Message[type](options)//再创建新的消息
  }
}

import tool from './utils/tool';
Object.keys(tool).forEach((key) => {
  Vue.prototype['$' + key] = tool[key];
});
Vue.prototype.$http = http;
Vue.prototype.$localData = data;
Vue.prototype.$message = new DonMessage()
Vue.prototype.$cookies = Cookies;
Vue.component(UTable.name, UTable);
Vue.component(UTableColumn.name, UTableColumn);
Vue.use(routerServer, { router, store });
Vue.config.productionTip = false;

const isDebugMode = false;
Vue.config.debug = isDebugMode;
Vue.config.devtools = isDebugMode;
Vue.config.productionTip = isDebugMode;

Vue.directive('removeAriaHidden', {
  bind(el, binding) {
    let ariaEls = el.querySelectorAll('.el-radio__original');
    ariaEls.forEach((item) => {
      item.removeAttribute('aria-hidden');
    });
  }
});


// 引入Dialog可拖拽，注意文件所在目录。目前尚未发现引入的先后关系，若有再补充
import './directives.js'

import ImageViewer from 'vue2-viewer'

Vue.use(ImageViewer)


// 导入axios
import axios from 'axios'

Vue.prototype.$get = function(url, data, header, callback) { // 这就相当于在Vue类库中添加了一个$get()方法
  axios({
    url: url,
    method: 'get',
    params: data, // params属性表示参数在url地址后面，SpringMvc可以通过RequestParam指定参数名进行读取
    headers: header
  }).then(function(resp) {
    if (typeof callback === 'function') {
      callback(resp)
    }
  }).catch(response => {})
}

Vue.prototype.$post = function(url, data, header, callback) { // 这就相当于在Vue类库中添加了一个$get()方法
  axios({
    url: url,
    method: 'post',
    data: data, // data表示参数在请求（body）中，后端可以通过流直接读取，SpringMvc可以使用RequestBody注解获取
    headers: header,
    dataType: 'json'
  }).then(function(resp) {
    if (typeof callback === 'function') {
      callback(resp)
    }
  }).catch(response => {})
}

// 用来文件下载的
Vue.prototype.$downFile = function(url, data, header, callback) { // 这就相当于在Vue类库中添加了一个$get()方法
  const token = {
    'Content-Type': 'multipart/form-data',
    'X-Token': getToken()
  }
  const headers = Object.assign(token, header)
  axios({
    url: url,
    method: 'post',
    data: data, // data表示参数在请求（body）中，后端可以通过流直接读取，SpringMvc可以使用RequestBody注解获取
    headers: headers,
    dataType: 'json',
    responseType: 'blob' // 在请求中加上这一行，特别重要,用于表示返回来的数据是文件流，否则解析文件会有乱码
  }).then(function(resp) {
    if (typeof callback === 'function') {
      callback(resp)
    }
  }).catch(response => {})
}

// 用来文件上传的
Vue.prototype.$upFile = function(url, data, header, callback) { // 这就相当于在Vue类库中添加了一个$get()方法
  const token = {
    'Content-Type': 'multipart/form-data',
    'X-Token': getToken()
  }
  const headers = Object.assign(token, header)
  axios({
    url: url,
    method: 'post',
    data: data, // data表示参数在请求（body）中，后端可以通过流直接读取，SpringMvc可以使用RequestBody注解获取
    headers: headers,
    dataType: 'json'
  }).then(function(resp) {
    if (typeof callback === 'function') {
      callback(resp)
    }
  }).catch(response => {})
}



new Vue({
  router,
  store,
  render: (h) => h(App),
}).$mount('#app');
